﻿Imports System
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.IO
Imports System.IO.Ports
Imports System.ComponentModel
Imports System.Windows.Forms
'**************************************************************
'ADC物理教學示範實驗 Server Ver. 1.0 Beta
'作者 : 胡晉豪
'日期 : 2007/12/31
'**************************************************************
Public Class Form1
    Const portNumber As Integer = 8000  '設定Port
    Dim Max_Thread As Integer = 12  '設定最大Client接收數
    Dim id As Integer = 1
    Dim tcpListener As New TcpListener(portNumber)
    Private trd(Max_Thread) As Thread
    Delegate Sub SetTextCallback(ByVal [text] As String)
    Private Sub ThreadTask()
        Try
            '----------------宣告----------------
            Dim tid As Integer = id
            Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            Dim bytes() As Byte
            Dim clientdata As String
            'Dim responseString As String = "ack"
            'Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
            '----------------宣告----------------
            Me.SetText("連線已建立")
            ReDim bytes(tcpClient.ReceiveBufferSize)
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            clientdata = Encoding.ASCII.GetString(bytes)
            While clientdata <> "End"
                'Me.SetText(clientdata.Length)
                Me.SetText(clientdata)
                'networkStream.Write(sendBytes, 0, sendBytes.Length)
                ReDim bytes(tcpClient.ReceiveBufferSize)
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                clientdata = Encoding.ASCII.GetString(bytes)
            End While
            tcpClient.Close()
            Me.SetText("連線已關閉")
        Catch e As Exception
            'Me.SetText(e.ToString())
        End Try
    End Sub
    Private Sub SetText(ByVal [text] As String)
        If Me.RichTextBox1.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf SetText)
            Me.Invoke(d, New Object() {[text]})
        Else
            [text] = [text] + vbCrLf
            Me.RichTextBox1.AppendText([text])
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        tcpListener.Start()
        RichTextBox1.AppendText("等待Client端連線中..." + vbCrLf)
        Dim i As Integer
        For i = 0 To Max_Thread - 1
            Me.trd(i) = New Thread(New ThreadStart(AddressOf Me.ThreadTask))
            Me.trd(i).Start()
            Thread.Sleep(50)
            id = id + 1
        Next
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim i As Integer
        For i = 0 To Max_Thread - 1
            Me.trd(i).Abort()
        Next
        tcpListener.Stop()
        RichTextBox1.AppendText("Server closed")
    End Sub
End Class